package com.liren.imgtool.tools;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 固定高度切割
 * @author: LiRen
 * @create time: on 2019-10-17 09:58
 **/

public class Fixed_Cut {

    public static void main(String[] args) {
        String imgUrl = "http://gzhimages.paidanzi.com/cut/df82e088-7236-4b76-a07f-9bc8f3307c2b/0_0.jpg";
//        String imgUrl = "http://gzhimages.paidanzi.com/Img/0da77783-0b6d-4235-9be9-0f44451864ee/1571295969574/1.jpg";
//        String imgUrl = "C:/lzz/data/0_0.jpg";
        Integer destWidth = 0;
        Integer destHeight = 0;
        getImage(imgUrl, destWidth, destHeight);
    }

    private static void getImage(String imgUrl, Integer destWidth, Integer destHeight) {
        try {

            List<String> list = new ArrayList<String>();
            String fileType = ".jpg";
            BufferedImage bi;
            URL urlImg = new URL(imgUrl);
            InputStream in = urlImg.openStream();
            bi = ImageIO.read(in);
            int height = bi.getHeight();
            Integer cols = 2;
            Integer rows = 4;
            if (height > 20000) {
                //  那么将按照2W一切。 如5W的高度。那么需要按照切三次。
                rows = height /20000 + 1;
            }
            int srcWidth = bi.getWidth();
            int srcHeight = bi.getHeight();
            destWidth = srcWidth / cols;    // j    500
            destHeight = 20000;   // i    2W

            System.out.println("destHeight = " + destHeight + "，destWidth：" + destWidth);
            //	循环建立切片
            System.out.println("rows = " + rows + ",cols: " + cols);
            for (int i = 0; i < rows; i++) {
                if (rows - 1 == i) {
                    System.out.println("srcHeight："+srcHeight + "-- " + i);
                    destHeight = srcHeight - destHeight * (i);
                    System.out.println("destHeight = " + destHeight);
                }
                for (int j = 0; j < cols; j++) {
                    // 四个参数分别为图像起点坐标和宽高
                    System.out.println("i = " + i +",i * destHeight," + i * destHeight);
                    System.out.println("j = " + j + "j * destWidth:" + j * destWidth);
                    Image image = bi.getSubimage(j * destWidth, i * 20000, destWidth, destHeight);
                    BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
                    Graphics g = tag.getGraphics();
                    g.drawImage(image, 0, 0, destWidth,  destHeight, null);
                    g.dispose();
                    ByteArrayOutputStream byOut = new ByteArrayOutputStream();
                    ImageIO.write(tag, "JPEG", byOut);
                    byte[] imgBytes = byOut.toByteArray();
                    String p = "C://lzz//cut//bal_" + i + "_" + j + ".png";
                    ImageIO.write(tag, "JPEG", new File(p));
                    System.out.println("p = " + p);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
